package com.sfx.concurrent.demo.concurrent.pattern.GuardedObjectEncapsulationTest;

import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:用于解耦 人和邮寄员的
 * User: sfx
 * Date: 2023-01-01
 * Time: 10:27
 */

public class MailBoxes {

    //把所有的GuardedObject全部封装起来->利用id唯一对应一个GuardedObject
    private static Map<Integer,GuardedObject> mailBox = new Hashtable<>();
    //唯一id
    private static int id;

    //产生唯一id
    private static synchronized int generate(){
        return id ++;
    }

    //得到所有的id
    public static Set<Integer> getAllId(){
        return mailBox.keySet();
    }

    //创建出唯一一个GuardedObject
    public static GuardedObject createGuardedObject(){
        GuardedObject guardedObject = new GuardedObject(generate());
        mailBox.put(guardedObject.getId(),guardedObject);
        return guardedObject;
    }

    //通过id获取到GuardedObject
    public static GuardedObject getGuardedObject(int id){
        return mailBox.remove(id);
    }
}